//**********************************************************
//    File Name: HomecareWorker.cs
//    Author: Team Motif
//    Date Created : 04/13/2007
//    Current Owner: Team Motif 
//                                                                            
//    Purpose: 
//    This class provides the functions needed to create, update, access, and delete a 
//    homecare worker.
//
//	  Copyright 2007
//**********************************************************

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Library
{
    // -------------------------------------------------------------------------------
    // Structure created to hold the contents of a client certification
    // -------------------------------------------------------------------------------
    public struct Certification
    {
        public int CertID;
        public String CertName;
        public String CertDescription;
    };
    public class HomeCareWorker : Person
    {

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// The default constructor of a HomeCareWorker.  
        /// -------------------------------------------------------------------------------
        /// </summary>
        public HomeCareWorker()
        {
        }

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// The partial constructor of a HomeCareWorker.  
        /// -------------------------------------------------------------------------------
        /// </summary>
        public HomeCareWorker(String firstName, String lastName)
        {
            this.SetName(firstName, lastName);
        }

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// The main constructor of a HomeCareWorker.  
        /// -------------------------------------------------------------------------------
        /// </summary>
        public HomeCareWorker(String firstName, String lastName, DateTime dateOfBirth)
        {
            this.SetName(firstName, lastName);
            this.SetDateOfBirth(dateOfBirth);
            
        }
        
        /// <summary>
        /// -------------------------------------------------------------------------------
        /// Sets the contraints of the homecare worker
        /// -------------------------------------------------------------------------------
        /// </summary>
        public void SetContraints(string constraints)
        {
            mConstraints = constraints;
        }

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// Sets the weekly hours of the homecare worker
        /// -------------------------------------------------------------------------------
        /// </summary>
        public void SetWeeklyHours(int weeklyHours)
        {
            mWeeklyHrs = weeklyHours;
        }

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// Set the certificate values for a HCW
        /// -------------------------------------------------------------------------------
        /// </summary>
        public static void SetCertificate(int tmpId, String tmpCertName, String tmpCertDesc)
        {
            System.Object lockThis = new System.Object();
            lock (lockThis)
            {
                Certification myCert;
                myCert.CertID = tmpId;
                myCert.CertName = tmpCertName;
                myCert.CertDescription = tmpCertDesc;
            }
        }
        /// <summary>
        /// -------------------------------------------------------------------------------
        /// Static global method which will return a worker based on a worker ID.  Will return
        /// Null if worker is not found
        /// -------------------------------------------------------------------------------
        /// </summary>
        public static HomeCareWorker GetWorkerByID(long id)
        {
            HomeCareWorker myWorker = new HomeCareWorker();
            System.Object lockThis = new System.Object();
            lock (lockThis)
            {
                myWorker = DBUtil.ReadWorkerByID(id);
            }
            return myWorker;
            
        }

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// Static global method which will return an array of Homecare worker objects based on availablity
        /// and tasks.
        /// Will Return null if a homecare worker is not found
        /// -------------------------------------------------------------------------------
        /// </summary>
        public static ArrayList GetWorkerByMatchAvailAndTask(AvailableTimeSlot preferredTime, ArrayList requestedTasks)
        {
            ArrayList homecareWorkers = null;

            System.Object lockThis = new System.Object();
            lock (lockThis)
            {
                try
                {
                    // List of homecare worker ID's that will be returned by the DB utility
                    ArrayList homecareWorkerIDs = new ArrayList();

                    //Call the corresponding funtion in the database utility to fetch the homecare workers from the database
                    homecareWorkerIDs = DBUtil.ReadMatchingAvailAndTasksWorker(preferredTime, requestedTasks);

                    // List of homecare workers that will be returned to the calling function
                    homecareWorkers = new ArrayList();

                    if (homecareWorkerIDs.Count != 0)
                    {
                        for (int i = 0; i < homecareWorkerIDs.Count; i++)
                        {
                            int workerID = (int)homecareWorkerIDs[i];
                            HomeCareWorker matchingWorker = GetWorkerByID(workerID);
                            homecareWorkers.Add(matchingWorker);
                        }
                    }
                }
                catch (Exception ExceptionMsg)
                {
                    LOG.error("HomecareWorker::GetWorkerByMatchAvailAndTask Exception handled: " + ExceptionMsg.ToString());
                }
            }   
            return homecareWorkers;            
        }

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// Static global method which will return a list of Workers based on a match of first
        /// and last name.  It is up to the caller to verity the list is not empty.
        /// -------------------------------------------------------------------------------
        /// </summary>
        public static ArrayList GetWorkerList(string firstName, string lastName)
        {
            System.Object lockThis = new System.Object();

            ArrayList HCWList = new ArrayList();
            lock (lockThis)
            {                
                HCWList = DBUtil.ReadHCWList(firstName, lastName);
            }
            return HCWList;
            
        }

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// Static global method which will return a Worker based on first name, last name, and date of birth.
        /// Will Return null if Worker is not found
        /// -------------------------------------------------------------------------------
        /// </summary>
        public static HomeCareWorker GetWorkerByMatch(String firstName, String lastName, DateTime dateOfBirth)
        {
            HomeCareWorker myWorker = null;

            System.Object lockThis = new System.Object();
            lock (lockThis)
            {
                // to be implemented
            }
             
            return myWorker;
            
        }

       
        /// <summary>
        /// -------------------------------------------------------------------------------
        /// Static global method which will store a Worker.  Returns true/false for success 
        /// -------------------------------------------------------------------------------
        /// </summary>
        public static bool SaveWorker(HomeCareWorker myWorker)
        {
            System.Object lockThis = new System.Object();
            lock (lockThis)
            {
                Utility DBUtil = new Utility();
                return (DBUtil.SaveWorker(myWorker));
            }
        }

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// Retrieves all the Workers in the database in an ArrayList collection
        /// -------------------------------------------------------------------------------
        /// </summary>
        public static ArrayList GetAllWorkers()
        {
            ArrayList workerList = new ArrayList();

            System.Object lockThis = new System.Object();
            lock (lockThis)
            {
                // To be implemented
            }
            return workerList;
            
        }

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// gets the worker id
        /// -------------------------------------------------------------------------------
        /// </summary>
        public long GetWorkerId()
        {
            return this.GetUserID();
        }

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// Getter for Constraints
        /// -------------------------------------------------------------------------------
        /// </summary>
        public String constraints
        {
            get { return mConstraints; }
            set { mConstraints = value; }
        }

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// Getter for WeeklyHrs
        /// -------------------------------------------------------------------------------
        /// </summary>
        public int weeklyHrs
        {
            get { return mWeeklyHrs; }
            set { mWeeklyHrs = value; }
        }

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// Method for retireving a worker's tasks
        /// -------------------------------------------------------------------------------
        /// </summary>
        /// <returns></returns>
        public ArrayList GetWorkerTasks()
        {
            return workerTasks;
        }

        
        /// <summary>
        /// -------------------------------------------------------------------------------
        /// Method for adding a new approved task for the worker
        /// -------------------------------------------------------------------------------
        /// </summary>
        public ArrayList AddNewTask(Task newTask)
        {
            workerTasks.Add(newTask);
            
            return workerTasks;
        }

        
        /// <summary>
        /// -------------------------------------------------------------------------------
        /// Method for adding a new Certification for the worker
        /// -------------------------------------------------------------------------------
        /// </summary>
        public bool AddNewCertification(Certification newCertification)
        {
            //this.Skills = new ArrayList();
            //this.Skills = DBUtil.AddNewWorkerSkill(newSkill); 

            return true;
        }

        
        /// <summary>
        /// -------------------------------------------------------------------------------
        /// Method for getting list of all Certification for the worker
        /// -------------------------------------------------------------------------------
        /// </summary>
        public static ArrayList GetCertificateForID(long id)
        {
            ArrayList CertList = new ArrayList();
            System.Object lockThis = new System.Object();
            lock (lockThis)
            {
                CertList = DBUtil.ReadCertsForId(id);
            }
             
            return CertList;
        }

        
        /// <summary>
        /// -------------------------------------------------------------------------------
        /// Method for getting Constraints and weekly hours for a worker
        /// -------------------------------------------------------------------------------
        /// </summary>
        public static HomeCareWorker GetConstWeekHoursForHCW(long id)
        {
            HomeCareWorker myWorker = new HomeCareWorker();

            System.Object lockThis = new System.Object();
            lock (lockThis)
            {
                myWorker = DBUtil.ReadConstraintWeekHoursForHCW(id);
            }
             
            return myWorker;
            
        }

        //-------------------------------------------------------------------------------
        // Private data region
        //-------------------------------------------------------------------------------
        String mConstraints;
        int mWeeklyHrs;
        ArrayList workerTasks = new ArrayList();
        ArrayList Appointments = new ArrayList();
        ArrayList Certifications = new ArrayList();
        static Utility DBUtil = new Utility();
    }
};
